<?php
class QueryCityHandler implements IHandler {
	/**
	 * <h2>日志对象</h2>
	 *
	 * @var Logger;
	 */
	private $_logger;
	
	/**
	 * <h2>构造函数</h2>
	 * <p>对象的初始化</p>
	 */
	public function __construct() {
		Logger::configure ( ROOT_PATH . 'config/log_config.xml' );
		$this->_logger = Logger::getLogger ( __CLASS__ );
	}
	
	/**
	 * <h2>处理前端根据省份查询城市</h2>
	 *
	 * @see IHandler::handle()
	 * @return void
	 */
	public function handle() {
		GF::setCookie ( 'ABC', rand ( 1, 10 ) );
		$request = new Request ();
		try {
			if (! $request->isAjax ()) {
				throw new ExceptionEx ();
			}
			$provinceId = new CINT ( $request->getGet ( 'prvId' ) );
			include_once ROOT_PATH . 'models/CityModel.class.php';
			$cityModel = new CityModel ();
			$cityLst = $cityModel->getCityByProvinceId ( $provinceId->getValue () );
			$first = true;
			$html = '';
			foreach ( $cityLst as $item ) {
				if ($first) {
					$html .= "<option value=\"{$item->id}\" selected=\"selected\">{$item->name}</option>\n";
					$first = false;
				} else {
					$html .= "<option value=\"{$item->id}\">{$item->name}</option>\n";
				}
			}
			// $this->_logger->debug($html);
			header ( "Content-Type:text/plain;charset=utf-8" );
			echo ($html);
		} catch ( ExceptionEx $e ) {
		}
	}
}